<template>
  <el-drawer
    :visible.sync="drawerVisible"
    :wrapper-closable="true"
    :close-on-press-escape="false"
    :with-header="false"
    append-to-body
    size="80%"
  >
    <div class="drawer-content">
      <div class="head">
        <Title title="关联成本" />
        <div class="supplier-step-container">
          <step
            :order-status="process"
            :step-list="stepList"
          />
        </div>
      </div>
      <div v-if="detailData" class="select-container">
        <div class="select-content">
          <SelectCostItem
            v-if="process === 0"
            ref="selectCostItemRef"
            :select-cost-list="selectCostList"
            :detail-data="detailData" />
          <OrderSelect
            v-if="process === 1"
            ref="selectOrderRef"
            v-model="stepTwoData"
            :step-one-data="stepOneData"
            :settle-id="detailData.id"
            is-add-cost
            @setStepId="setStepId"
            @selectCallback="selectCallback"
          />
        </div>
        <div class="btn-box">
          <el-button
            size="small"
            type="primary"
            @click="nextStep">{{ process === 1?'上一步':'下一步' }}</el-button>
          <el-button
            v-if="process === 1"
            size="small"
            type="primary"
            @click="submitCostAndOrder">确认添加</el-button>
          <el-button
            class="back-btn cancel-btn-style"
            type="info"
            size="small"
            @click="closeDrawer"
          >返回列表</el-button>
        </div>
      </div>
    </div>
  </el-drawer>
</template>
<script>
import { supplierAddCost } from '@/api/supplier';
import SelectCostItem from './selectCostItem';
import OrderSelect from '../../model/orderSelect/index';
import Step from '@/components/CreateOrder/orderDetail/step';
import Title from '@/components/Title';
import ElMessage from '@/utils/message';
import { showModal } from '@/utils/message';

export default {
  components: { SelectCostItem, Step, Title, OrderSelect },
  props: {
    detailData: {
      type: Object,
      default: () => { return {}; }
    }
  },
  data() {
    return {
      drawerVisible: false,
      process: 0,
      stepList: [
        { name: '成本选择', id: 0 },
        { name: '审批项选择', id: 1, notFinish: true }
      ],
      selectCostList: [],
      // 步骤2 审批项选择的数据
      stepTwoData: {
        cost: []
      }
    };
  },
  computed: {
    stepOneData() {
      return {
        selectCost: this.selectCostList,
        costBelong: this.detailData.costBelong, // 成本归属 0:销售成本 2:萌力成本 6:有赞成本
        supplierId: this.detailData.supplierId, // 供应商id
        settlementMethod: this.detailData.settlementMethod || 0, // 结算方式
        isInvite: this.detailData.isInvite // 协作方式
      };
    }
  },
  created() {
  },
  methods: {
    openDrawer() {
      this.drawerVisible = true;
    },
    closeDrawer() {
      this.drawerVisible = false;
    },
    setStepId({ process }) {
      if (process || process === 0) this.process = process;
    },
    async nextStep() {
      try {
        if (this.process === 0) {
          const selectCostList = await this.$refs.selectCostItemRef.getResult();
          this.selectCostList = selectCostList;
          this.process = 1;
        } else {
          this.process = 0;
        }
      } catch (error) {
        console.log(error);
      }
    },
    async submitCostAndOrder() {
      try {
        if (this.$refs.selectOrderRef) {
          await this.$refs.selectOrderRef.submitCost();
        }
      } catch (error) {
        console.log(error);
      }
    },
    // 订单选择后调用
    selectCallback({ selectCost }) {
      showModal('关联的成本无法进行删除操作，请确认关联成本。').then(_ => {
        const params = {
          settleId: this.detailData.id,
          cost: selectCost
        };
        supplierAddCost(params).then(res => {
          if (res.code === 200) {
            this.closeDrawer();
            this.$emit('updateDetail');
          } else {
            ElMessage.error(res.message);
          }
        });
      });
    }
  }
};
</script>

<style lang="scss" scoped>
.drawer-content {
  padding: 24px 0 24px 24px;
  height: 100%;
  display: flex;
  flex-direction: column;
  &::v-deep {
    .order-select-container {
      padding: 0 !important;
    }
  }
}
.supplier-step-container {
  margin-top: 8px;
  margin-bottom: 8px;
  &::v-deep {
    .order-detail-step {
      padding: 0;
    }
  }
}
.select-container {
  flex: 1;
  height: 0px;
  .select-content {
    padding-right: 24px;
    height: calc(100% - 40px);
    @include scrollBar($x: hidden, $y: auto, $width: 6px);
  }
  .btn-box {
    display: flex;
    padding-top: 10px;
    align-items: center;
  }
}
</style>
